<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login System</title>
    <!-- 引入Vue和Axios -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        .login-container {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
        }
    </style>
</head>
<body>
    <div id="app" class="login-container">
        <h2>登录系统</h2>
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" v-model="username" placeholder="请输入用户名">
        </div>
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" v-model="password" placeholder="请输入密码">
        </div>
        <button @click="login">登录</button>
        <div v-if="message" class="message" :class="{success: isSuccess, error: !isSuccess}">
            {{ message }}
        </div>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    username: '',
                    password: '',
                    message: '',
                    isSuccess: false
                }
            },
            methods: {
                async login() {
                    // 简单验证输入不为空
                    if (!this.username || !this.password) {
                        this.message = '用户名和密码不能为空';
                        this.isSuccess = false;
                        return;
                    }

                    try {
                        // 发送POST请求到新的后端登录接口
                        const res = await axios.post('http://139.196.141.180/login', {
                            username: this.username,
                            password: this.password
                        });
                        
                        // 处理成功响应，根据后端返回的实际数据调整
                        this.message = res.data.message;
                        this.isSuccess = true;
                    } catch (error) {
                        // 处理错误情况
                        if (error.response) {
                            // 服务器返回错误状态码
                            this.message = error.response.data.message;
                        } else {
                            // 网络错误或其他异常
                            this.message = '网络或服务器错误，请稍后再试';
                        }
                        this.isSuccess = false;
                    }
                }
            }
        })
    </script>
</body>
</html>